/******************************************************************
Copyright © PsuCtl 2017~2024. All rights reserved.
* @projectName   PsuCtl
* @author        Raiiwhen
* @email         masterraii@icloud.com
*******************************************************************/
#ifndef _PMBUS_H_
#define _PMBUS_H_

#include "driver/i2c_master.h"

typedef struct
{
    bool init;
    i2c_master_bus_handle_t bus;
    uint8_t address;
    uint8_t romAddr;
    uint32_t freq;
    uint32_t timeout;
    // device info
    uint8_t mfrID[6];
    // device status
    float V_out;
    float PwrOut;
    float V_in;
    float I_in;
} smbus_info_t;

// PMBus I2C Address define
#define PMBUS_ADDRESS 0x58
// PMBus 命令定义
// 基本信息
#define MFR_ID 0x99
#define MFR_MODEL 0x9A
#define MFR_REVISION 0x9B
#define MFR_LOCATION 0x9C
#define MFR_DATE 0x9D
#define MFR_SERIAL 0x9E
// 规格信息
#define MFR_VIN_MIN 0xA0
#define MFR_VIN_MAX 0xA1
#define MFR_IIN_MAX 0xA2
#define MFR_PIN_MAX 0xA3
#define MFR_VOUT_MIN  0xA4
#define MFR_VOUT_MAX  0xA5
#define MFR_IOUT_MAX  0xA6
#define MFR_POUT_MAX  0xA7
#define MFR_TAMBIENT_MAX  0xA8
#define MFR_TAMBIENT_MIN  0xA9
// 输入输出信息
#define READ_VIN 0x88
#define READ_IIN 0x89
#define READ_VCAP 0x8A
#define READ_VOUT 0x8B
#define READ_IOUT 0x8C
#define READ_POUT 0x96
#define READ_PIN 0x97
// 温度相关
#define READ_TEMPERATURE_1 0x8D
#define READ_TEMPERATURE_2 0x8E
#define READ_TEMPERATURE_3 0x8F
#define READ_FAN_SPEED_1 0x90
// 参数设置
#define VOUT_COMMAND  0x21
#define VOUT_MAX  0x24
#define FAN_COMMAND_1 0x3B
#define VOUT_OV_FAULT_LIMIT 0x40
#define VOUT_OV_FAULT_RESPONSE 0x41
#define VOUT_OV_WARN_LIMIT  0x42
#define VOUT_UV_FAULT_LIMIT 0x44
#define VOUT_UV_FAULT_RESPONSE  0x45
#define IOUT_OC_FAULT_LIMIT 0x46
#define IOUT_OC_FAULT_RESPONSE  0x47
#define IOUT_OC_WARN_LIMIT 0x4A
#define OT_FAULT_LIMIT  0x4F
#define OT_FAULT_RESPONSE 0x50
#define OT_WARN_LIMIT 0x51
#define VIN_OV_FAULT_LIMIT  0x55
#define VIN_UV_W ARN_LIMIT  0x58
#define VIN_UV_FAULT_LIMIT  0x59


#define COEFFICIENTS 0x30
#define VOUT_MODE 0x20

void PMBusConfig(uint8_t devAddr, uint8_t romAddr, i2c_master_bus_handle_t bus, uint32_t freq);
void PMBusRegRead8Bit(uint8_t reg, uint8_t* val);
void PMBusRegRead16Bit(uint8_t reg, uint16_t* val);
void PMBusRegReadRom(uint8_t reg, uint8_t* val, uint8_t len);

void PMBusSyncInfo();
void PMBusSyncStatus();
void PMBusSyncStatusPrint();
const smbus_info_t* GetPMBusInfoInstance();


#endif